﻿Public Class frmDollarRoomsBE
    Private da As New DSVaultingTableAdapters.ir_dollar_roomsTableAdapter
    Private action As String
    Private actType As String() = {"NONE", "INSERT", "UPDATE", "DELETE"}
    Private slcId As Long

    Private Sub frmDollarRoomsBE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        uxIRdgv.DataSource = da.GetData
    End Sub

    Private Sub frmDollarRoomsBE_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmSetting.Enabled = True
    End Sub

    Private Sub uxNewRoomBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxNewRoomBtn.Click
        uxRoomNameTbx.Enabled = True
        uxRoomNameTbx.Text = ""
        action = actType(1)
    End Sub

    Private Sub uxEditRoomBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEditRoomBtn.Click
        If uxIRdgv.SelectedRows.Count > 0 Then
            uxRoomNameTbx.Enabled = True
            uxRoomNameTbx.Text = uxIRdgv.SelectedRows.Item(0).Cells(1).Value
            If Not uxRoomNameTbx.Text = "" Then
                slcId = uxIRdgv.SelectedRows.Item(0).Cells(0).Value
                action = actType(2)
            End If
        End If
    End Sub

    Private Sub uxDeleteRoomBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxDeleteRoomBtn.Click
        If MessageBox.Show("តើអ្នកពិតជាចង់លុបចោលទិន្នន័យនេះមែនទេ?", "សារបញ្ជាក់ពីការលុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            Dim oldId As Long = uxIRdgv.SelectedRows.Item(0).Cells(0).Value
            da.Delete(oldId, uxIRdgv.SelectedRows.Item(0).Cells(1).Value)
            Me.updateDGV()
        End If
    End Sub

    Private Sub updateDGV()
        uxIRdgv.DataSource = da.GetData
    End Sub

    Private Sub uxSaveChangeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxSaveChangeBtn.Click
        Dim val As String = uxRoomNameTbx.Text
        If Not val = "" Then
            If action = actType(1) Then
                da.Insert(val)
                uxRoomNameTbx.Clear()
                updateDGV()
            ElseIf action = actType(2) Then
                If MessageBox.Show("តើអ្នកពិតជាចងរក្សាទុកព័ត៌មានដែលបានកែប្រែមែនទេ?", "សារបញ្ជាក់ពីការកែប្រែ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    da.Update(val, slcId)
                    updateDGV()
                End If
            End If
        End If
    End Sub

    Private Sub uxIRdgv_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles uxIRdgv.CellDoubleClick
        uxEditRoomBtn_Click(sender, e)
    End Sub

    Private Sub uxRoomNameTbx_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles uxRoomNameTbx.KeyPress
        If e.KeyChar = ChrW(Keys.Enter) Then
            uxSaveChangeBtn_Click(sender, e)
        End If
    End Sub

    Private Sub uxCancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCancelBtn.Click
        frmSetting.Enabled = True
        Me.Close()
    End Sub
End Class